<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.File" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.activiti.engine.ProcessEngine" %>
<%@ page import="org.activiti.engine.RepositoryService" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="com.JavascriptMessage" %>
<%@ page import="com.DateUtil" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<link type="text/css" rel="stylesheet" charset="utf-8" href="<%=request.getContextPath() %>/css/base.css"/>
<title>Deploy</title>
</head>
<%
String message = null;

WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);
//ProcessEngine engine = (ProcessEngine)context.getBean("processEngine");
RepositoryService repository = (RepositoryService)context.getBean("repositoryService");

String doWhat = request.getParameter("doWhat");
if(doWhat != null && doWhat.equals("upload") && ServletFileUpload.isMultipartContent(request)){
	FileItemFactory factory = new DiskFileItemFactory();
	ServletFileUpload upload = new ServletFileUpload(factory);
	List<FileItem> files = upload.parseRequest(request);
	/*
	String realPath = application.getRealPath("/").replace("\\", "/");
	File file = new File(realPath + "uploadFiles/");
	if(!file.exists())
		file.mkdirs();
	*/
	for(FileItem item : files){
		if(!item.isFormField()){
			/*
			String fullFileName = realPath + "uploadFiles/" + item.getName();
			File uploadFile = new File(fullFileName);
			item.write(uploadFile);
			*/
			if(item.getName().contains("bpmn20.xml")){
				System.out.println("upload file is process file, deploying...");
				repository
					.createDeployment()
					.addInputStream(item.getName(), item.getInputStream())
					.name(item.getName() + "-" + DateUtil.getDateTime())
					.deploy();
				message = "deploy success...";
			}else{
				message = "deploy failed, the file is not bpmn20.xml!";
			}
		}
	}

}
%>
<body>
<%if(message != null){out.write(JavascriptMessage.alert(message));}%>
<form action="<%=request.getContextPath() %>/jsp/deploy.jsp?doWhat=upload" method="post" enctype="multipart/form-data">
	<input type="file" name="file"/>
	<input type="submit" value="submit"/>
</form>
</body>
</html>